package com.jhhc.BDMS.Data.Controller;

import com.jhhc.BDMS.Data.Pojo.*;
import com.jhhc.BDMS.Data.Service.*;
import com.jhhc.RHDB.Data.Pojo.Data;
import com.jhhc.utils.ImageBase64;
import com.jhhc.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Api(value="水库信息服务",tags={"水库信息服务"})
@RestController
@RequestMapping(value = "BDMS/Data")
public class RelationCurveController {

    @Resource(name = "BDMS_DD_RSVRHQ_BService")
    private BDMS_DD_RSVRHQ_BService BDMS_DD_RSVRHQ_BService;
    @Resource(name = "BDMS_ST_ZQRL_BService")
    private BDMS_ST_ZQRL_BService BDMS_ST_ZQRL_BService;

    @Resource(name = "BDMS_ST_ZVARL_BService")
    private BDMS_ST_ZVARL_BService BDMS_ST_ZVARL_BService;
    @Resource(name = "BDMS_ST_PPTN_EService")
    private BDMS_ST_PPTN_EService bdms_st_pptn_eService;

    @Resource(name = "bDMS_ST_RIVER_EService")
    private BDMS_ST_RIVER_EService bdms_st_river_eService;

    @Resource(name = "bDMS_ST_RSVR_EService")
    private BDMS_ST_RSVR_EService bdms_st_rsvr_eService;

    @ApiOperation(value = "泄流能力关系曲线", notes = "泄流能力关系曲线",httpMethod = "GET")
    @RequestMapping(value = "/GetHQRL", method = RequestMethod.GET)
    public JsonResult GetCurve(@ApiParam(name = "PID", value = "", required = true) @RequestParam Integer PID,
                               @ApiParam(name = "RSVRCD", value = "", required = true) @RequestParam String RSVRCD){
        JsonResult jsonResult = new JsonResult();
        Curve curve = new Curve();
        GatageZQ gatageZQ = null;
        StageDischarge stageDischarge = null;
        List<BDMS_DD_RSVRHQ_B> BDMS_DD_RSVRHQ_BS = BDMS_DD_RSVRHQ_BService.getCurve(PID,RSVRCD);
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < BDMS_DD_RSVRHQ_BS.size(); i++) {
            List<GatageZQ> gatageZQList = new ArrayList<>();
            //if (BDMS_DD_RSVRHQ_BS.get(i).getPid() != null)curve.setPid(BDMS_DD_RSVRHQ_BS.get(i).getPid());
            if (BDMS_DD_RSVRHQ_BS.get(i).getRsvrcd() != null)curve.setRsvrcd(BDMS_DD_RSVRHQ_BS.get(i).getRsvrcd());
            if (!list.contains(BDMS_DD_RSVRHQ_BS.get(i).getGatage())) {
                list.add(BDMS_DD_RSVRHQ_BS.get(i).getGatage());
            }
            for (int j = 0; j < list.size(); j++) {
                gatageZQ = new GatageZQ();
                List<StageDischarge> stageDischargeList = new ArrayList<>();
                int finali = j;
                List<BDMS_DD_RSVRHQ_B> datas = BDMS_DD_RSVRHQ_BS.stream()
                        .filter(r->list.get(finali).contains(r.getGatage())).collect(Collectors.toList());
                for (int k = 0; k < datas.size(); k++) {
                    stageDischarge = new StageDischarge();
                    if (datas.get(k).getGatage() != null) gatageZQ.setGatage(datas.get(k).getGatage());
                    if (datas.get(k).getH() != null) stageDischarge.setH(datas.get(k).getH());
                    if (datas.get(k).getQ() != null) stageDischarge.setQ(datas.get(k).getQ());
                    stageDischargeList.add(stageDischarge);
                }
                gatageZQ.setStageDischarges(stageDischargeList);
                gatageZQList.add(gatageZQ);
            }
            curve.setGatageZQs(gatageZQList);
        }
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        jsonResult.setData(curve);
        return jsonResult;
    }

    @ApiOperation(value = "水位流量关系曲线", notes = "水位流量关系曲线",httpMethod = "GET")
    @RequestMapping(value = "/GetZQRL", method = RequestMethod.GET)
    public JsonResult GetCurveData(@ApiParam(name = "PID", value = "", required = true) @RequestParam Integer PID,
                                   @ApiParam(name = "STCD", value = "", required = true) @RequestParam String STCD){
        JsonResult jsonResult = new JsonResult();
        CurveData curveData = null;
        CurveDataZQ curveDataZQ = null;
        List<CurveDataZQ> curveDataZQList = new ArrayList<>();
        List<BDMS_ST_ZQRL_B> BDMS_ST_ZQRL_BS = BDMS_ST_ZQRL_BService.getCurveData(PID, STCD);
        for (int i = 0; i < BDMS_ST_ZQRL_BS.size(); i++) {
            curveData = new CurveData();
            curveDataZQ = new CurveDataZQ();
            //if (BDMS_DD_RSVRHQ_BS.get(i).getPid() != null)curve.setPid(BDMS_DD_RSVRHQ_BS.get(i).getPid());
            if (BDMS_ST_ZQRL_BS.get(i).getStcd() != null)curveData.setStcd(BDMS_ST_ZQRL_BS.get(i).getStcd());
            if (BDMS_ST_ZQRL_BS.get(i).getLnnm() != null)curveData.setLnnm(BDMS_ST_ZQRL_BS.get(i).getLnnm());
            if (BDMS_ST_ZQRL_BS.get(i).getBgtm() != null)curveData.setBgtm(BDMS_ST_ZQRL_BS.get(i).getBgtm());

            if (BDMS_ST_ZQRL_BS.get(i).getZ() != null)curveDataZQ.setZ(BDMS_ST_ZQRL_BS.get(i).getZ());
            if (BDMS_ST_ZQRL_BS.get(i).getQ() != null)curveDataZQ.setQ(BDMS_ST_ZQRL_BS.get(i).getQ());
            curveDataZQList.add(curveDataZQ);
            curveData.setCurveDataZQs(curveDataZQList);
        }
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        jsonResult.setData(curveData);
        return jsonResult;
    }


    @ApiOperation(value = "水位库容关系曲线", notes = "水位库容关系曲线",httpMethod = "GET")
    @RequestMapping(value = "/GetZVARL", method = RequestMethod.GET)
    public JsonResult GetCotwl(@ApiParam(name = "PID", value = "", required = true) @RequestParam Integer PID,
                               @ApiParam(name = "STCD", value = "", required = true) @RequestParam String STCD){
        JsonResult jsonResult = new JsonResult();
        ImageBase64 imageBase64 = new ImageBase64();
        WaterLevelStorageCapacityRelationship wlscr = null;
        CapacityOfTheWaterLevel cotwl = null;
        List<CapacityOfTheWaterLevel> cotwlList = new ArrayList<>();
        List<BDMS_ST_ZVARL_B> BDMS_ST_ZVARL_BS = BDMS_ST_ZVARL_BService.getCotwl(PID, STCD);
        for (int i = 0; i < BDMS_ST_ZVARL_BS.size(); i++) {
            wlscr = new WaterLevelStorageCapacityRelationship();
            cotwl = new CapacityOfTheWaterLevel();
            if (BDMS_ST_ZVARL_BS.get(i).getStcd() != null)wlscr.setStcd(BDMS_ST_ZVARL_BS.get(i).getStcd());
            if (BDMS_ST_ZVARL_BS.get(i).getMstm() != null)wlscr.setMstm(BDMS_ST_ZVARL_BS.get(i).getMstm());
            if (BDMS_ST_ZVARL_BS.get(i).getPtno() != null)cotwl.setPtno(BDMS_ST_ZVARL_BS.get(i).getPtno());
            double w = BDMS_ST_ZVARL_BS.get(i).getW();
            cotwl.setW(imageBase64.doubleAccuracy2(w));
            double rz = BDMS_ST_ZVARL_BS.get(i).getRz();
            cotwl.setRz(imageBase64.doubleAccuracy2(rz));
            if (BDMS_ST_ZVARL_BS.get(i).getWsfa() != null)cotwl.setWsfa(BDMS_ST_ZVARL_BS.get(i).getWsfa());
            cotwlList.add(cotwl);
            wlscr.setCapacityOfTheWaterLevel(cotwlList);
        }
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        jsonResult.setData(wlscr);
        return jsonResult;
    }


    @ApiOperation(value = "水位库容转换",notes = "水位库容转换",httpMethod = "GET")
    @RequestMapping(value = "/GetCotwlTransformation",method = RequestMethod.GET)
    public JsonResult GetCotwlTransformation(@ApiParam(name = "STCD",value = "") @RequestParam String STCD
            ,@ApiParam(name = "rzw",value = "") @RequestParam double rzw
            ,@ApiParam(name = "rzwType",value = "") @RequestParam RZWType rzwType){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setStatusCode(200);
        jsonResult.setSucceeded(true);
        try {
            ImageBase64 imageBase64 = new ImageBase64();
            List<BDMS_ST_ZVARL_B> bdms_st_zvarl_bs = BDMS_ST_ZVARL_BService.getCotwlTransformation(STCD);
            bdms_st_zvarl_bs.sort(Comparator.comparingDouble(BDMS_ST_ZVARL_B::getRz));
            int front = 0;
            int after = 0;
            double x1 = 0;
            double y1 = 0;
            double x2 = 0;
            double y2 = 0;
            switch (rzwType){
                case rz:
                    for (int i = 0; i < bdms_st_zvarl_bs.size(); i++) {
                        if (rzw < bdms_st_zvarl_bs.get(i).getRz())  {
                            front = i - 1;
                            after = i;
                            break;
                        }
                    }
                    if (front >= 0 && bdms_st_zvarl_bs.get(front) != null){
                        x1 = bdms_st_zvarl_bs.get(front).getRz();
                        y1 = bdms_st_zvarl_bs.get(front).getW();
                    }
                    if (bdms_st_zvarl_bs.get(after) != null){
                        x2 = bdms_st_zvarl_bs.get(after).getRz();
                        y2 = bdms_st_zvarl_bs.get(after).getW();
                    }
                    double w = imageBase64.W(x1,y1,x2,y2,rzw);
                    jsonResult.setStatusCode(200);
                    jsonResult.setSucceeded(true);
                    jsonResult.setData(w);
                    break;
                case w:
                    for (int i = 0; i < bdms_st_zvarl_bs.size(); i++) {
                        if (rzw < bdms_st_zvarl_bs.get(i).getW())  {
                            front = i - 1;
                            after = i;
                            break;
                        }
                    }
                    if (bdms_st_zvarl_bs.get(front) != null){
                        x1 = bdms_st_zvarl_bs.get(front).getRz();
                        y1 = bdms_st_zvarl_bs.get(front).getW();
                    }
                    if (bdms_st_zvarl_bs.get(after) != null){
                        x2 = bdms_st_zvarl_bs.get(after).getRz();
                        y2 = bdms_st_zvarl_bs.get(after).getW();
                    }
                    double rz = imageBase64.RZ(x1,y1,x2,y2,rzw);
                    jsonResult.setData(rz);
                    break;
            }
        }catch (Exception e){
            jsonResult.setStatusCode(200);
            jsonResult.setSucceeded(true);
            jsonResult.setData(null);
        }
        return jsonResult;
    }



    //数据维护
    @ApiOperation(value = "数据维护",notes = "数据维护",httpMethod = "POST")
    @RequestMapping(value = "/InsertOrUpdateData",method = RequestMethod.POST)
    public JsonResult InsertOrUpdate(@RequestBody IUParams iuParams){
        JsonResult jsonResult = new JsonResult();
        Integer pid = iuParams.getProjectId();
        //解析iu
        //循环数据
        int res = 0;
        for (IUParam iuParam : iuParams.getIuParams()) {
            String stationCode = iuParam.getStationCode();

            List<BDMS_ST_RIVER_E> river_eList = new ArrayList<>();
            List<BDMS_ST_RSVR_E> rsvr_eList = new ArrayList<>();
            // 使用Map来存储融合后的数据
            Map<String, BDMS_ST_RIVER_E> dataMap = new HashMap<>();
            Map<String, BDMS_ST_RSVR_E> dataMapRSVR = new HashMap<>();
            switch (iuParam.getSttp()){
                case "PP":
                    List<BDMS_ST_PPTN_E> pptn_eList = new ArrayList<>();
                    for (IUParamData value :iuParam.getDatas()) {
                        BDMS_ST_PPTN_E st_pptn_e = new BDMS_ST_PPTN_E();
                        st_pptn_e.setStcd(stationCode);
                        st_pptn_e.setPid(pid);
                        st_pptn_e.setTm(value.getTm());
                        st_pptn_e.setDrp(value.getValue());
                        pptn_eList.add(st_pptn_e);
                    }
                    //入库
                    if (pptn_eList.size() > 0) {
                        res =  bdms_st_pptn_eService.insertOrUpdateBatch(pptn_eList);
                    }
                    break;

                case "ZQ":
                    for (IUParamData value :iuParam.getDatas()) {
                        String currentTime = value.getTm();
                        BDMS_ST_RIVER_E bdms_st_river_e ;
                        if (dataMap.containsKey(currentTime)) {
                            bdms_st_river_e = dataMap.get(currentTime);
                        } else {
                            bdms_st_river_e = new BDMS_ST_RIVER_E();
                            dataMap.put(currentTime, bdms_st_river_e);
                        }

                        if ("Z".equals(value.getSensorType())) {
                            bdms_st_river_e.setStcd(stationCode);
                            bdms_st_river_e.setPid(pid);
                            bdms_st_river_e.setTm(currentTime);
                            bdms_st_river_e.setZ(value.getValue());
                        } else if ("Q".equals(value.getSensorType())) {
                            bdms_st_river_e.setQ(value.getValue());
                        }
                    }
                    river_eList= new ArrayList<>(dataMap.values());
                    //入库
                    if (river_eList.size() > 0) {
                        System.out.println(river_eList);
                      res =  bdms_st_river_eService.insertOrUpdateBatch(river_eList);
                    }
                    break;
                case "RR":
                    for (IUParamData value :iuParam.getDatas()) {
                        String currentTime = value.getTm();

                        BDMS_ST_RSVR_E rsvr_e;

                        if (dataMap.containsKey(currentTime)) {
                            rsvr_e = dataMapRSVR.get(currentTime);
                        } else {
                            rsvr_e = new BDMS_ST_RSVR_E();
                            rsvr_e.setPid(pid);
                            rsvr_e.setStcd(stationCode);
                            rsvr_e.setTm(currentTime);
                            dataMapRSVR.put(currentTime, rsvr_e);
                        }

                        // 根据 sensorType 设置对应的值
                        switch (value.getSensorType()) {
                            case "Inq":
                                rsvr_e.setInq(value.getValue());
                                break;
                            case "Otq":
                                rsvr_e.setOtq(value.getValue());
                                break;
                            case "RZ":
                                rsvr_e.setRz(value.getValue());
                                break;
                            case "W":
                                rsvr_e.setW(value.getValue());
                                break;
                            default:
                                // handle unknown sensorType
                                break;
                        }
                    }
                    rsvr_eList= new ArrayList<>(dataMapRSVR.values());
                    //入库
                    if (rsvr_eList.size() > 0) {
                     res =   bdms_st_rsvr_eService.insertOrUpdateBatch(rsvr_eList);
                    }
                    break;
                default:
                    break;
            }

        }
        if (res>0){
            jsonResult.setData("操作成功");
            jsonResult.setRemark("操作成功");
            jsonResult.setStatusCode(200);
            jsonResult.setSucceeded(true);
        }else {
            jsonResult.setData("操作失败");
            jsonResult.setRemark("操作失败");
            jsonResult.setStatusCode(201);
            jsonResult.setSucceeded(false);
        }

        return  jsonResult;
    }


}
